package com.fable.sp.core.config;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.propertyeditors.PropertiesEditor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;

/**
 * .
 *
 * @author stormning on 16/9/9.
 */
public class CustomWebBindingInitializer implements WebBindingInitializer {
    @Override
    public void initBinder(WebDataBinder binder, WebRequest request) {
        binder.registerCustomEditor(double.class, new DoubleEditor());
        binder.registerCustomEditor(Double.class, new DoubleEditor(true));
        binder.registerCustomEditor(long.class, new LongEditor());
        binder.registerCustomEditor(Long.class, new LongEditor(true));
        binder.registerCustomEditor(int.class, new IntegerEditor());
        binder.registerCustomEditor(Integer.class, new IntegerEditor(true));
        binder.registerCustomEditor(float.class, new FloatEditor());
        binder.registerCustomEditor(Float.class, new FloatEditor(true));
    }

    public abstract static class NumberEditor extends PropertiesEditor {
        boolean returnNull = false;

        public NumberEditor() {
        }

        public NumberEditor(boolean returnNull) {
            this.returnNull = returnNull;
        }

        @Override
        public String getAsText() {
            return ObjectUtils.getDisplayString(getValue());
        }

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            boolean number = NumberUtils.isNumber(text);
            if (number) {
                internalSetAsText(text);
            } else {
                if (returnNull) {
                    setValue(null);
                } else {
                    internalSetAsText("0");
                }
            }
        }

        protected abstract void internalSetAsText(String text);
    }


    public static class DoubleEditor extends NumberEditor {
        public DoubleEditor() {
        }

        public DoubleEditor(boolean returnNull) {
            super(returnNull);
        }

        @Override
        protected void internalSetAsText(String text) {
            setValue(NumberUtils.toDouble(text));
        }
    }

    public static class IntegerEditor extends NumberEditor {
        public IntegerEditor() {
        }

        public IntegerEditor(boolean returnNull) {
            super(returnNull);
        }

        @Override
        protected void internalSetAsText(String text) {
            setValue(NumberUtils.toInt(text));
        }
    }

    public static class FloatEditor extends NumberEditor {
        public FloatEditor() {
        }

        public FloatEditor(boolean returnNull) {
            super(returnNull);
        }

        @Override
        protected void internalSetAsText(String text) {
            setValue(NumberUtils.toFloat(text));
        }
    }

    public static class LongEditor extends NumberEditor {
        public LongEditor() {
        }

        public LongEditor(boolean returnNull) {
            super(returnNull);
        }

        @Override
        protected void internalSetAsText(String text) {
            setValue(NumberUtils.toLong(text));
        }
    }
}